from __future__ import absolute_import
from __future__ import print_function
import os
from PIL import Image
import numpy as np
import cv2
import h5py
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.models import model_from_json
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.advanced_activations import PReLU
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD, Adadelta, Adagrad
from keras.utils import np_utils, generic_utils
import random
imagepaths='/work/ocr/resource/charSamples/ModelImg'
length = sum([len(x) for _, _, x in os.walk(os.path.dirname(imagepaths))])
print('filelength=', length)
data = np.empty((length, 1, 23, 13), dtype="float32")
label = np.empty((length,), dtype="uint8")
i = 0
for root, sub_dirs, files in os.walk(imagepaths):

    for special_file in files:
        if special_file.startswith(".DS_Store"):
            continue;
        if special_file.endswith('.jpg'):
            filepath = os.path.join(root, special_file)
            image = cv2.imread(filepath, cv2.IMREAD_COLOR)
            image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            #retval, imarray = cv2.threshold(image_gray, 170, 255, cv2.THRESH_BINARY)
            newpath=os.path.join(root, "erzhi_"+special_file)
            im1=Image.fromarray(image_gray);
            im1.save(newpath)
